#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2023/8/11 13:55
# @Name    : 同.py
# @email   : will05150707@163.com
# @Author  : wei.yan
import os
from datetime import datetime

from docx import Document
from pptx import Presentation


def add_word_to_ppt_notes(word_file, ppt_file):
    # 打开 Word 文档
    doc = Document(word_file)

    # 打开现有的 PPT 文档
    prs = Presentation(ppt_file)

    current_slide_index = -1
    current_content = []

    # 遍历 Word 文档中的每个段落
    for paragraph in doc.paragraphs:
        if paragraph.style.name.startswith('Heading 1'):
            if current_slide_index < len(prs.slides):
                # 将之前收集的正文内容添加到幻灯片的备注中
                add_content_to_slide(current_content, prs.slides[current_slide_index])
                current_slide_index += 1
            current_content = []  # 清空正文内容
        else:
            current_content.append(paragraph.text)  # 收集正文内容

    # 处理最后一页的正文内容
    if current_slide_index < len(prs.slides):
        add_content_to_slide(current_content, prs.slides[current_slide_index])

    merge_file_name = os.path.splitext(word_file)[0] + "_" + os.path.splitext(ppt_file)[0] + "_" + datetime.now().strftime(
        '%Y%m%d%H%M%S') + ".pptx"
    merged_file = os.path.join(os.path.dirname(__file__), merge_file_name)

    # 保存修改后的 PPT 文档
    prs.save(merged_file)


def add_content_to_slide(content, slide):
    notes_slide = slide.notes_slide
    notes_shape = notes_slide.notes_text_frame

    notes_shape.text = '\n'.join(content)


if __name__ == '__main__':
    add_word_to_ppt_notes('人工智能.docx', '人工智能.pptx')
